#ifndef V4L2_H
#define V4L2_H

#include <QMainWindow>

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/mman.h>
#include <signal.h>
#include <poll.h>
#include <linux/fb.h>
#include <stdlib.h>
#include <string.h>

#include <QTimer>
#include <showphoto.h>


#define CMD_PID     "pgrep makevideo"
#define	video_width		640
#define video_height	480

#define myinfo(format, args...)           \
    do{                                   \
        printf("\033[1;42m");             \
        printf("[v4l2] ");                \
        printf(format, ##args);           \
        printf("\033[0m\n");              \
    }while(0)

#define myerror(format, args...)          \
    do{                                   \
        printf("\033[1;41m");             \
        printf("[v4l2] ");                \
        printf(format, ##args);           \
        printf("\033[0m\n");              \
    }while(0)

QT_BEGIN_NAMESPACE
namespace Ui { class v4l2; }
QT_END_NAMESPACE


class v4l2 : public QMainWindow
{
    Q_OBJECT

public:
    v4l2(QWidget *parent = nullptr);
    ~v4l2();

private slots:
    void on_pushButton_open_clicked();
    void on_pushButton_take_clicked();
    void on_pushButton_photos_clicked();
    void on_pushButton_video_clicked();
    void on_pushButton_timing_clicked();
    void on_comboBox_period_currentTextChanged(const QString &arg1);

    void update_time();
    void video_show();
    void take_photo();


private:
    Ui::v4l2 *ui;

    char *userbuff[4];        /* 存放摄像头设备内核缓冲区映射后的用户内存地址 */
    int userbuff_length[4];   /* 保存映射后的数据长度，释放缓存时要用 */

    int show_flag = 1;    /* 1:保存到本地   2:显示在lcd上 */
    int video_fd;
    int lcd_fd;
    int lcd_xres, lcd_yres;
    int lcd_realvirtual;

    int time_period = 1000;    //默认定时拍摄周期为1s
    bool open_camera = false;  //true:打开相机；false:关闭相机
    bool taking_photo = false; //true:正在定时拍照；false:没有定时拍照
    bool finish_video = true;  //true:视频合成完成；false:视频合成未完成
    static bool handler_sign;

    QTimer *timer_updatetime = nullptr;
    QTimer *timer_show = nullptr;
    QTimer *timer_shoot = nullptr;

    int v4l2_open();
    int v4l2_close();
    static void sig_handler(int sig);
};
#endif // V4L2_H
